package com.itheima.day2.exercise;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class Exercise1 {

    static List<Integer> filter(List<Integer> list) {
        List<Integer> result = new ArrayList<>();
        for (Integer number : list) {
            // 筛选：判断是否是偶数，但以后可能改变筛选规则
            // (Integer number) -> (number & 1) == 0
            if ((number & 1) == 0) {
                result.add(number);
            }
        }
        return result;
    }

    static List<Integer> filter(List<Integer> list, Predicate<Integer> predicate) {
        List<Integer> result = new ArrayList<>();
        for (Integer number : list) {
            // 筛选：判断是否是偶数，但以后可能改变筛选规则
            if (predicate.test(number)) {
                result.add(number);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<Integer> filter = filter(List.of(1, 2, 3, 4, 5), (Integer number) -> (number & 1) == 0);
        System.out.println(filter);
    }

}
